<?php

/**
 * contrato actions.
 *
 * @package    saveenergy
 * @subpackage contrato
 * @author     0
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class contratoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    /*
    $this->contratos = Doctrine_Core::getTable('Contrato')
      ->createQuery('a')
      ->execute();
    */
       $q = Doctrine_Query::create()
      ->from('Contrato c')
      ->where('c.is_firmado = true');
      //->where('c.fechaRevision > ?', date('Y-m-d', time()));
      $this->contratos = $q->execute();
       
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->contrato = Doctrine_Core::getTable('Contrato')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->contrato);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new AnadirContratoForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->form = new ContratoForm();
    $this->processForm($request, $this->form);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($contrato = Doctrine_Core::getTable('Contrato')->find(array($request->getParameter('id'))), sprintf('Object contrato does not exist (%s).', $request->getParameter('id')));
    $this->form = new ContratoForm($contrato);
    $this->contratoNuevo = Doctrine_Core::getTable('Contrato')->find("1"); //Tipo Doctrine_table
//  $contrato2 = Doctrine_core::createTablesFromArray($array); //Con esto podemos crear nuestro propio array de formulario
//  $this->form = new ContratoForm($contrato2);
    
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($contrato = Doctrine_Core::getTable('Contrato')->find(array($request->getParameter('id'))), sprintf('Object contrato does not exist (%s).', $request->getParameter('id')));
    $this->form = new ContratoForm($contrato);
    $this->processForm($request, $this->form);
    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();
    $this->forward404Unless($contrato = Doctrine_Core::getTable('Contrato')->find(array($request->getParameter('id'))), sprintf('Object contrato does not exist (%s).', $request->getParameter('id')));
    $contrato->delete();
    $this->redirect('contrato/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $contrato = $form->save();
      //$this->redirect('contrato/edit?id='.$contrato->getId());
      
      $this->redirect('contrato/index');
    }
  }
}
